import Vue from 'vue';
import Request from "@/sdk/pocky-request/index.js";
var vm=new Vue();
/**
 * 用户模块store
 */
const user = {
	// 状态
	state: {
		isLogin: false,
		userInfo: null,
		token: null,
		msgCount:{
				'0':0,
				'1':{
					unRead:0,
					newCount:0
				},
				'2':{
					unRead:0,
					newCount:0
				},
				'3':{
					unRead:0,
					newCount:0
				},
				'4':{
					unRead:0,
					newCount:0
				}
			},
		allCount:0,
		parentId:0
	},
	// 内部派生
	getters: {

	},
	// 同步方法 commit
	mutations: {
		isLogin: (state, payload) => {
			state.isLogin = payload
		},
		userInfo: (state, payload) => {
			state.userInfo = payload
		},
		token: (state, payload) => {
			state.token = payload;
			uni.setStorageSync('token', payload)
		},
		eidtTel: (state, payload) => {
			state.userInfo.username = payload
		},
		parentId: (state, payload) => {
			state.parentId = payload
		},
		msgCount: (state, payload) => {
			let _old=uni.getStorageSync("msgCount");
			_old=_old?_old:{
				'0':getApp().globalData.timCnt,
				'1':{
					unRead:0,
					newCount:0
				},
				'2':{
					unRead:0,
					newCount:0
				},
				'3':{
					unRead:0,
					newCount:0
				},
				'4':{
					unRead:0,
					newCount:0
				}
			}
			let _arr={
				'0':getApp().globalData.timCnt,
				'1':{
					unRead:0,
					newCount:0
				},
				'2':{
					unRead:0,
					newCount:0
				},
				'3':{
					unRead:0,
					newCount:0
				},
				'4':{
					unRead:0,
					newCount:0
				}
			}
			
			if(payload.length){	
				let _cut=0		//
				for(let i=1;i<5;i++){
					if(payload[i.toString()]){
						let _obj={
							unRead:payload[i.toString()].cnt-_old[i.toString()].newCount+_old[i.toString()].unRead,
							newCount:payload[i.toString()].cnt
						}
						_cut=_cut+_obj.unRead;
						_arr[i]=_obj
					}
				}
				uni.setStorageSync('msgCount',_arr);
				state.msgCount= _arr;		//接口消息
				state.allCount=_cut;		//接口消息总数
			}else{
				state.msgCount= _arr;
				state.allCount=0;
			}
			
		},
		apiMsgTap:(state, payload) => {
			state.allCount=state.allCount-state.msgCount[payload.toString()].unRead
			state.msgCount[payload.toString()].unRead=0
			uni.setStorageSync('msgCount',state.msgCount);
			setTimeout(()=>{
				uni.$emit("msgCountChange")
			},500)
		}
		
	},
	// 异步方法 dispatch
	actions: {
		/**
		 * @param {Object} context
		 * @param {Object} _data
		 * _data:{
			 username
			 password
		 }
		 * 
		 */
		apiUserLogin(context, _data) {
			return new Promise(resolve => {
				Request().request({
					url: '/common/login.do',
					method: 'post',
					data: _data
				}).then(res => {
					if (res.data.code == 1) {
						this.commit("userInfo", res.data.data);
						this.commit("token", res.data.data.token);
						this.commit("isLogin", true);
						resolve(res)
					} else {
						uni.showToast({
							title: '登录失败,' + res.data.msg,
							icon: 'none'
						});
					}
				})
			})
		},
		/**
		 * @description  自动登录
		 * @param {Object} context
		 * @param {Object} _data:{token:'xxxxx'}
		 */
		apiUserAutoLogin(context, _data) {
			return new Promise((resolve, reject) => {
				Request().request({
					url: '/common/login/auto.do',
					method: 'post',
					data: _data
				}).then(res => {
					if (res.data.code == 1) {
						this.commit("userInfo", res.data.data);
						this.commit("token", res.data.data.token);
						this.commit("isLogin", true);
						resolve(res)
					} else {
						uni.showToast({
							title: '您的登录信息已过期,请重新登录',
							icon: 'none'
						});
						reject(res)
					}
				})
			})
		},
		/**
		 * @description 用户注册
		 * @param {Object} context
		 * @param {Object} _data
		 * _data:{
			"username"
			"password"
			"code":
		 }
		 */
		apiUserRegister(context, _data) {
			return new Promise((resolve, reject) => {
				Request().request({
					url: '/common/regist.do',
					method: 'post',
					data: _data
				}).then(res => {
					if (res.data.code == 1) {
						this.commit("userInfo", res.data.data);
						this.commit("token", res.data.data.token);
						this.commit("isLogin", true);
						vm.$store.dispatch("checkActionReporo").then(()=>{
							vm.$store.dispatch("getToken").then(()=>{
								vm.$store.dispatch("reportAction",{
									acType:'REGISTER',
									userId:res.data.data.userId
								})
								
							})
						})
						resolve(res)
					} else {
						uni.showToast({
							title: '注册失败',
							icon: 'none'
						});
						reject(res)
					}
				})
			})
		},
		/**
		 * @description 发送短信验证码
		 * @param {Object} context
		 * @param {Object} _data
		 * //1.注册 2.登录 3.修改密码 4.验证原手机号 5.修改手机号
		 */
		apiSendMsgCode(context, _data) {
			return new Promise((resolve, reject) => {
				Request().request({
					url: '/common/send_sms.do',
					method: 'post',
					data: _data
				}).then(res => {
					if (res.data.code == 1) {
						resolve(res)
					} else {
						uni.showToast({
							title: '验证码发送失败,' + res.data.msg,
							icon: 'none'
						});
						reject(res)
					}
				}).catch(err => {
					uni.showToast({
						title: '验证码发送失败,' + res.data.msg,
						icon: 'none'
					});
					reject(res)
				})
			})
		},
		/**
		 * @description 修改密码
		 * @param {Object} context
		 * @param {Object} _data
		 */
		apiEditPass(context, _data) {
			return new Promise((resolve, reject) => {
				Request().request({
					url: '/user/modify_password.do',
					method: 'post',
					data: _data
				}).then(res => {
					if (res.data.code == 1) {
						resolve(res)
					} else {
						uni.showToast({
							title: '' + res.data.msg,
							icon: 'none'
						});
						reject(res)
					}
				}).catch(err => {
					uni.showToast({
						title: '' + res.data.msg,
						icon: 'none'
					});
					reject(res)
				})
			})
		},
		/**
		 * @description 退出登录
		 * @param {Object} context
		 * @param {Object} _data
		 */
		apiLogOut(context, _data) {
			return new Promise((resolve, reject) => {
				Request().request({
					url: '/common/logout.do',
					method: 'post',
					data: _data
				}).then(res => {
					if (res.data.code == 1) {
						uni.removeStorageSync("token");
						this.commit("userInfo", null);
						this.commit("token", null);
						this.commit("isLogin", false);
						resolve(res)
					} else {
						reject(res)
					}
				}).catch(err => {
					uni.showToast({
						title: '用户信息修改失败,' + res.data.msg,
						icon: 'none'
					});
					reject(res)
				})
			})
		},
		/**
		 * @description 修改用户信息
		 * @param {Object} context
		 * @param {Object} _data
		 */
		eidtUserInfo(context, _data) {
			_data.city=parseInt(context.rootState.map.locationCity.adcode);
			_data.version=context.rootState.upDate.nowAppInfo.version;
			return new Promise((resolve, reject) => {
				Request().request({
					url: '/user/update_info.do',
					method: 'post',
					data: _data
				}).then(res => {
					if (res.data.code == 1) {
						resolve(res)
					} else {
						uni.showToast({
							title: '用户信息修改失败,' + res.data.msg,
							icon: 'none'
						});
						reject(res)
					}
				}).catch(err => {
					uni.showToast({
						title: '用户信息修改失败,' + res.data.msg,
						icon: 'none'
					});
					reject(res)
				})
			})
		},
		/**
		 * @description 修改手机号
		 * @param {Object} context
		 * @param {Object} _data
		 */
		apiEditTel(context, _data) {
			return new Promise((resolve, reject) => {
				Request().request({
					url: '/user/update_phone.do',
					method: 'post',
					data: _data
				}).then(res => {
					if (res.data.code == 1) {
						resolve(res)
						this.commit("eidtTel", _data.phone)
					} else {
						uni.showToast({
							title: '修改失败,' + res.data.msg,
							icon: 'none'
						});
						reject(res)
					}
				}).catch(err => {
					uni.showToast({
						title: '修改失败,' + res.data.msg,
						icon: 'none'
					});
					reject(res)
				})
			})
		},
		/**
		 * @description 获取学员列表
		 * @param {Object} context
		 * @param {Object} _data
		 */
		apiGetMemberList(context, _data) {
			return new Promise((resolve, reject) => {
				Request().request({
					url: '/user/member/list.do',
					method: 'post',
					data: _data
				}).then(res => {
					if (res.data.code == 1) {
						resolve(res)
					} else {
						reject(res)
					}
				}).catch(err => {
					reject(res)
				})
			})
		},
		loginByWexin(context) {
			return new Promise((resolve, reject) => {
				uni.login({
					provider: 'weixin',
					success: (loginRes) => {
						console.log(loginRes);
						uni.getUserInfo({
							provider: 'weixin',
							success: (infoRes) => {
								resolve(infoRes);
							}
						});
					}
				});
			})

		},
		loginByQQ(context) {
			return new Promise((resolve, reject) => {
				uni.login({
					provider: 'qq',
					success: (loginRes) => {
						console.log(loginRes);
						uni.getUserInfo({
							provider: 'qq',
							success: (infoRes) => {
								resolve(infoRes);
							}
						});
					}
				});
			})

		},
		loginByWeiBo(context) {
			return new Promise((resolve, reject) => {
				uni.login({
					provider: 'sinaweibo',
					success: (loginRes) => {
						console.log(loginRes);
						uni.getUserInfo({
							provider: 'sinaweibo',
							success: (infoRes) => {
								resolve(infoRes);
							},
							fail: (err) => {
								console.log(err);
							}
						});
					}
				});
			})

		},
		/**
		 * 通过APPLE 登录
		 * @param {Object} context
		 */
		loginByApple(context) {

			return new Promise((resolve, reject) => {
				uni.login({
					provider: 'apple',
					success: (loginRes) => {
						console.log(loginRes);
						uni.getUserInfo({
							provider: 'apple',
							success: (infoRes) => {
								console.log(infoRes);
								resolve(infoRes);
							},
							fail: (err) => {
								console.log(err);
							}
						})
					},
					fail: (err) => {
						console.log(err);
					}
				});
			})

		},
		/**
		 * @description 第三方登录
		 * @param {Object} context
		 * @param {Object} _data
		 * loginType:1微信   2qq   3微博  4apple
		 */
		apiThirdLogin(context, _data) {
			return new Promise((resolve, reject) => {
				Request().request({
					url: '/common/loginOpen.do',
					method: 'post',
					data: _data
				}).then(res => {
					if (res.data.code == 1) {
						this.commit("userInfo", res.data.data);
						this.commit("token", res.data.data.token);
						this.commit("isLogin", true);
						resolve(res)
					} else {
						reject(res)
					}

				}).catch(err => {
					reject(res)
				})
			})
		},
		/**
		 * @description 门店统计
		 * @param {Object} context
		 * @param {Object} _data
		 */
		apiPostTongji(context, _data) {
			return new Promise((resolve, reject) => {
				Request().request({
					url: '/store/stat/add.do',
					method: 'post',
					data: _data
				}).then(res => {
					resolve(res)
				}).catch(err => {
					reject(res)
				})
			})
		},
		
		/**添加学员
		 * @param {Object} context
		 * @param {Object} _data
		 */
		apiAddMember(context, _data) {
			return new Promise((resolve, reject) => {
				Request().request({
					url: '/user/member/add.do',
					method: 'post',
					data: _data
				}).then(res => {
					if (res.data.code == 1) {
						resolve(res)
					} else {
						uni.showToast({
							title: '添加失败',
							icon: 'none'
						});
						reject(res)
					}
				}).catch(err => {
					reject(res)
				})
			})
		},
		/**修改学员
		 * @param {Object} context
		 * @param {Object} _data
		 */
		apiEditMember(context, _data) {
			return new Promise((resolve, reject) => {
				Request().request({
					url: '/user/member/update.do',
					method: 'post',
					data: _data
				}).then(res => {
					if (res.data.code == 1) {
						resolve(res)
					} else {
						uni.showToast({
							title: '修改失败',
							icon: 'none'
						});
						reject(res)
					}
				}).catch(err => {
					reject(res)
				})
			})
		},
		/**删除学员
		 * @param {Object} context
		 * @param {Object} _data
		 */
		apiRemoveMember(context, _data) {
			return new Promise((resolve, reject) => {
				Request().request({
					url: '/user/member/update.do',
					method: 'post',
					data: _data
				}).then(res => {
					resolve(res)
				}).catch(err => {
					reject(res)
				})
			})
		},
		/**
		 * @description 绑定支付宝
		 * @param {Object} context
		 * @param {Object} _data
		 */
		apiBindAlipay(context, _data) {
			return new Promise((resolve, reject) => {
				Request().request({
					url: '/group/aliBind.do',
					method: 'post',
					data: _data
				}).then(res => {
					resolve(res)
				}).catch(err => {
					reject(res)
				})
			})
		},
		/**提现
		 * @param {Object} context
		 * @param {Object} _data
		 */
		apiWithdrawal(context, _data) {
			return new Promise((resolve, reject) => {
				Request().request({
					url: '/group/fetch.do',
					method: 'post',
					data: _data
				}).then(res => {
					resolve(res)
				}).catch(err => {
					reject(res)
				})
			})
		},
		/**
		 * @description 获取我的关注
		 * @param {Object} context
		 * @param {Object} _data+
		 */
		apiGetMyCollection(context, _data) {
			return new Promise((resolve, reject) => {
				Request().request({
					url: '/user/fav/list.do',
					method: 'post',
					data: _data
				}).then(res => {
					resolve(res)
				}).catch(err => {
					reject(res)
				})
			})
		},
		/**
		 * @description 意见反馈
		 * @param {Object} context
		 * @param {Object} _data
		 */
		apiFeedBack(context,_data){
			return new Promise((resolve, reject) => {
				Request().request({
					url: '/common/feedback/add.do',
					method: 'post',
					data: _data
				}).then(res => {
					resolve(res)
				}).catch(err => {
					reject(res)
				})
			})
		},
		/**获取消息列表
		 * @param {Object} context
		 * @param {Object} _data
		 */
		async apiGetNociteList(context,_data){
			return new Promise(resolve => {
				let r = Request();
				r.request({
					url: '/msg/list.do',
					method: 'post',
					data: _data
				}).then(res=>{
					if(res.data.code==1){
						resolve(res)
						
					}else{
						uni.showToast({
							title: '消息列表获取失败,'+res.data.msg,
							icon: 'none'
						});
					}
				})
			})
		},
		/**
		 * @description  获取我的优惠券列表
		 * @param {Object} context
		 * @param {Object} _data
		 */
		apiGetMyCouponList(context,_data){
			return new Promise(resolve => {
				let r = Request();
				r.request({
					url: '/coupon/user/list.do',
					method: 'post',
					data: _data
				}).then(res=>{
					if(res.data.code==1){
						resolve(res)
						
					}else{
						uni.showToast({
							title: '优惠券列表获取失败,'+res.data.msg,
							icon: 'none'
						});
					}
				})
				
			})
		},
		/**
		 * @description 获取课领取优惠券列表
		 * @param {Object} context
		 * @param {Object} _data
		 */
		apiGetCatchCouponList(context,_data){
			return new Promise(resolve => {
				let r = Request();
				r.request({
					url: '/coupon/get/list.do',
					method: 'post',
					data: _data
				}).then(res=>{
					if(res.data.code==1){
						resolve(res)
						
					}else{
						uni.showToast({
							title: '可领取优惠券列表获取失败,'+res.data.msg,
							icon: 'none'
						});
					}
				})
				
			})
		},
		/**
		 * @description 领取新人礼包
		 * @param {Object} context
		 * @param {Object} _data
		 */
		apiGetNewGift(context,_data){
			return new Promise(resolve => {
				let r = Request();
				r.request({
					url: '/coupon/regist/fetch.do',
					method: 'post',
					data: _data
				}).then(res=>{
					if(res.data.code==1){
						uni.showToast({
							title: '领取成功',
							icon: 'none'
						});
						resolve(res)
						
					}else{
						uni.showToast({
							title: '领取失败,'+res.data.msg,
							icon: 'none'
						});
					}
				})
				
			})
		},
		/**
		 * @description 获取学员成长记录
		 * @param {Object} context
		 * @param {Object} _data
		 */
		apiGetMemberLog(context,_data){
			return new Promise(resolve => {
				let r = Request();
				r.request({
					url: '/user/memberlog/list.do',
					method: 'post',
					data: _data
				}).then(res=>{
					if(res.data.code==1){
						resolve(res)
						
					}else{
						uni.showToast({
							title: '优惠券列表获取失败,'+res.data.msg,
							icon: 'none'
						});
					}
				})
				
			})
		},
		/**
		 * @description  新增/编辑学员成长记录
		 * @param {Object} context
		 * @param {Object} _data
		 */
		apiPostMemberLog(context,_data){
			return new Promise(resolve => {
				let r = Request();
				r.request({
					url: '/user/memberlog/save.do',
					method: 'post',
					data: _data
				}).then(res=>{
					if(res.data.code==1){
						resolve(res)
						
					}else{
						uni.showToast({
							title: '保存失败,'+res.data.msg,
							icon: 'none'
						});
					}
				})
				
			})
		},
		/**
		 * @description 删除学员成长记录
		 * @param {Object} context
		 * @param {Object} _data
		 */
		apiDelMemberLog(context,_data){
			return new Promise(resolve => {
				let r = Request();
				r.request({
					url: '/user/memberlog/delete.do',
					method: 'post',
					data: _data
				}).then(res=>{
					if(res.data.code==1){
						resolve(res)
						
					}else{
						uni.showToast({
							title: '删除记录失败,'+res.data.msg,
							icon: 'none'
						});
					}
				})
				
			})
		},
		/**
		 * @description 获取消息总数
		 * @param {Object} context
		 * @param {Object} _data
		 */
		apiGetUnreadMsg(context,_data){
			return new Promise(resolve => {
				let r = Request();
				r.request({
					url: '/msg/stat.do',
					method: 'post',
					data: _data
				}).then(res=>{
					if(res.data.code==1){
					
						resolve(res)
						
					}else{
						uni.showToast({
							title: '获取消息总数失败,'+res.data.msg,
							icon: 'none'
						});
					}
				})
				
			})
		},
		/**获取课表
		 * @param {Object} context
		 * @param {Object} _data
		 */
		apiGetClassTable(context,_data){
			return new Promise(resolve => {
				let r = Request();
				r.request({
					url: '/user/classtable/list.do',
					method: 'post',
					data: _data
				}).then(res=>{
					if(res.data.code==1){
					
						resolve(res)
						
					}else{
						uni.showToast({
							title: '课表获取失败,'+res.data.msg,
							icon: 'none'
						});
					}
				})
				
			})
		},
		/**
		 * 获取某一天的点名状态
		 * @param {Object} context
		 * @param {Object} _data
		 */
		apiGetRollStatus(context,_data){
			return new Promise(resolve => {
				let r = Request();
				r.request({
					url: '/user/timeable/stat.do',
					method: 'post',
					data: _data
				}).then(res=>{
					if(res.data.code==1){
					
						resolve(res)
						
					}else{
						uni.showToast({
							title: '点名状态获取失败,'+res.data.msg,
							icon: 'none'
						});
					}
				})
				
			})
		},
		/**
		 * 获取考勤签到学员
		 * @param {Object} context
		 * @param {Object} _data
		 */
		apiGetRollMember(context,_data){
			return new Promise(resolve => {
				let r = Request();
				r.request({
					url: '/user/timeable/student.do',
					method: 'post',
					data: _data
				}).then(res=>{
					if(res.data.code==1){
					
						resolve(res)
						
					}else{
						uni.showToast({
							title: '获取签到学员失败,'+res.data.msg,
							icon: 'none'
						});
					}
				})
				
			})
		},
		/**
		 * 获取用户课次列表
		 * @param {Object} context
		 * @param {Object} _data
		 */
		apiGetClassTimeList(context,_data){
			return new Promise(resolve => {
				let r = Request();
				r.request({
					url: '/user/orderTime/list.do',
					method: 'post',
					data: _data
				}).then(res=>{
					if(res.data.code==1){
					
						resolve(res)
						
					}else{
						uni.showToast({
							title: '课次列表获取失败,'+res.data.msg,
							icon: 'none'
						});
					}
				})
				
			})
		},
		/**
		 * 获取用户点名记录
		 * @param {Object} context
		 * @param {Object} _data
		 */
		apiGetRollRecord(context,_data){
			return new Promise(resolve => {
				let r = Request();
				r.request({
					url: '/checkin/listByOrderId.do',
					method: 'post',
					data: _data
				}).then(res=>{
					if(res.data.code==1){
					
						resolve(res)
						
					}else{
						uni.showToast({
							title: '课次列表获取失败,'+res.data.msg,
							icon: 'none'
						});
					}
				})
				
			})
		},
		/**
		 * 获取学员可请假课程
		 * @param {Object} context
		 * @param {Object} _data
		 */
		apiGetMemberClass(context,_data){
			return new Promise(resolve => {
				let r = Request();
				r.request({
					url: '/leave/order/list.do',
					method: 'post',
					data: _data
				}).then(res=>{
					if(res.data.code==1){
						resolve(res)
					}else{
						uni.showToast({
							title: '可请假课程获取失败,'+res.data.msg,
							icon: 'none'
						});
					}
				})
				
			})
		},
		/**
		 * @description 申请请假
		 * @param {Object} context
		 * @param {Object} _data
		 */
		apiApplyLeave(context,_data){
			return new Promise(resolve => {
				let r = Request();
				r.request({
					url: '/leave/add.do',
					method: 'post',
					data: _data
				}).then(res=>{
					if(res.data.code==1){
						resolve(res)
					}else{
						uni.showToast({
							title: '申请请假失败,'+res.data.msg,
							icon: 'none'
						});
					}
				})
				
			})
		},
		/**
		 * @description 
		 * @param {Object} context
		 * @param {Object} _data
		 */
		apiGetLeaveList(context,_data){
			return new Promise(resolve => {
				let r = Request();
				r.request({
					url: '/leave/list.do',
					method: 'post',
					data: _data
				}).then(res=>{
					if(res.data.result==1){
						resolve(res)
					}else{
						uni.showToast({
							title: '请假列表获取失败,'+res.data.msg,
							icon: 'none'
						});
					}
				})
				
			})
		},
		/**加入团队
		 * @param {Object} context
		 * @param {Object} _data
		 */
		apiJoinTeam(context,_data){
			return new Promise(resolve => {
				let r = Request();
				r.request({
					url: '/agent/partner.do',
					method: 'post',
					data: _data
				}).then(res=>{
					if(res.data.result==1){
						resolve(res)
					}else{
						uni.showToast({
							title: '加入团队失败,'+res.data.msg,
							icon: 'none'
						});
					}
				})
				
			})
		},
		/**获取我的团队成员
		 * @param {Object} context
		 * @param {Object} _data
		 */
		apiGetTeamList(context,_data){
			return new Promise(resolve => {
				let r = Request();
				r.request({
					url: '/agent/listMember.do',
					method: 'post',
					data: _data
				}).then(res=>{
					if(res.data.result==1){
						resolve(res)
					}else{
						uni.showToast({
							title: '获取团队列表失败,'+res.data.msg,
							icon: 'none'
						});
					}
				})
				
			})
		},
		/**
		 * 获取分销佣金列表
		 * @param {Object} context
		 * @param {Object} _data
		 */
		apiGetDistionComm(context,_data){
			return new Promise(resolve => {
				let r = Request();
				r.request({
					url: '/agent/award/list.do',
					method: 'post',
					data: _data
				}).then(res=>{
					if(res.data.result==1){
						resolve(res)
					}else{
						uni.showToast({
							title: '获取团队列表失败,'+res.data.msg,
							icon: 'none'
						});
					}
				})
				
			})
		}
	}
}

export default user
